import 'package:flutter/material.dart';
import 'package:flutter_application_1/plugins/push.dart';
import 'package:flutter_application_1/utils/file_utils.dart';
import 'package:flutter_application_1/utils/utils.dart';
import 'package:flutter_downloader/flutter_downloader.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:jpush_flutter/jpush_flutter.dart';
import 'package:sn_progress_dialog/progress_dialog.dart';

class NotificationPushTest extends StatefulWidget {
  @override
  _NotificationPushTestState createState() => _NotificationPushTestState();
}

class _NotificationPushTestState extends State<NotificationPushTest> {
  // String downloadUrl = 'https://dldir1.qq.com/weixin/android/weixin806android1900_arm64.apk';

  String downloadUrl =
      'https://thirdwx.qlogo.cn/mmopen/vi_32/ZCkthDMhtd7a0AmPd0SZ36zqeGqKwKGj36oiaO9pjcYy4EMDWbbelTKe6GajpNJHlpDb2x9ScnXvsONBE5ibrR4A/132';

  late ProgressDialog _progressDialog;

  @override
  void initState() {
    super.initState();

    WidgetsBinding.instance!.addPostFrameCallback((_) {
      _progressDialog = ProgressDialog(
        context: context,
      );
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('通知推送'),
      ),
      body: SingleChildScrollView(
        child: Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Column(
              children: [
                Row(
                  children: [
                    Text('注册id：${Push.registrationID}'),
                    TextButton(
                      child: Text('复制'),
                      onPressed: () {
                        Utils.copyToClipboard(Push.registrationID ?? '').then(
                            (value) => Fluttertoast.showToast(msg: '复制成功'));
                      },
                    ),
                  ],
                ),
                ElevatedButton(
                  child: Text('本地推送'),
                  onPressed: () async {
                    int id = DateTime.now().millisecondsSinceEpoch ~/ 1000;
                    Push.jpush.sendLocalNotification(
                      LocalNotification(
                        id: id,
                        buildId: 1,
                        title: 'notification title$id',
                        content: 'notification content$id',
                        // 触发时间
                        fireTime: DateTime.now(),
                        // 该参数只有在 iOS 有效
                        subtitle: 'notification subtitle$id',
                        // 该参数只有在 iOS 有效
                        badge: 5,
                        extra: {
                          'type': '本地推送',
                        },
                      ),
                    );
                  },
                ),
                TextButton(
                  child: Text('清空推送'),
                  onPressed: () async {
                    Push.jpush.clearAllNotifications();
                  },
                ),
                ElevatedButton(
                  child: Text('下载测试'),
                  onPressed: () {
                    // Fluttertoast.showToast(msg: '开始下载');

                    _progressDialog.show(
                      max: 100,
                      msg: '下载点东西...',
                      barrierDismissible: true,
                    );

                    FileUtils.saveFile(
                      filePath: downloadUrl,
                      showNotification: true,
                      openFileFromNotification: true,
                      callback: (String taskId, DownloadTaskStatus status,
                          int progerss) {
                        _progressDialog.update(value: progerss);

                        if (status == DownloadTaskStatus.failed) {
                          _progressDialog.close();
                          Fluttertoast.showToast(msg: '下载失败');
                        } else if (status == DownloadTaskStatus.complete) {
                          _progressDialog.close();
                          showDialog(
                            context: context,
                            builder: (BuildContext context) {
                              return AlertDialog(
                                content: Text('下载完成是否打开？'),
                                actions: [
                                  TextButton(
                                    child: Text('取消'),
                                    onPressed: () => Navigator.pop(context),
                                  ),
                                  TextButton(
                                    child: Text(
                                      '确定',
                                      style: TextStyle(color: Colors.blue),
                                    ),
                                    onPressed: () {
                                      FlutterDownloader.open(taskId: taskId);
                                      Navigator.pop(context);
                                    },
                                  ),
                                ],
                              );
                            },
                          );
                        }
                      },
                    );
                  },
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }
}
